/**
 * 说明：
 * 1.meta中 isRoot 代表是否为侧边菜单栏目录
 */

import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import store from "@/store";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "Layout",
    redirect:'/workbench/dashboard',
    component: ()=>import("@/layout/Index.vue"),
  },

  {
    path: "/login",
    name: "Login",
    component: () =>import("@/views/Login.vue"),
  },

  {
    path: "/workbench",
    name: "WorkBench",
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"首页"
    },
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/workBench/Index.vue"),
        meta: {
          title: "工作台",
          isRoot:true,
        },
      },
    ]
  },

  {
    path: '/report',
    name: 'Report',
    component: () => import('@/layout/Index.vue'),
    redirect: '/report/list',
    meta: {
      title:"研报管理"
    },
    children: [
      {
        path: "list",
        name: "ReportList",
        component: () => import('@/views/report/List.vue'),
        meta: {
          title: "研报列表",
          isRoot:true,
        },
      },
      {
        path: "add",
        name: "AddReport",
        component: () => import('@/views/report/AddReport.vue'),
        meta: {
          title: "添加研报",
          isRoot:false,
        },
      }
    ]
  },

  {
    path: "/contract",
    name: "Contract",
    component: () => import("@/layout/Index.vue"),
    redirect:"/contract/list",
    meta: {
      title:"合同管理"
    },
    children: [
      {
        path: "list",
        name: "ContractList",
        component: () => import("@/views/contract/List.vue"),
        meta: {
          title: "合同列表",
          isRoot:true,
        },
      },
      // id 合同id
      {
        path: "detail/:id",
        name: "ContractDetail",
        component: () => import("@/views/contract/Detail.vue"),
        meta: {
          title: "合同详情",
          isRoot:false,
        },
      },
      // 添加合同
      {
        path: "add",
        name: "AddContract",
        component: () => import("@/views/contract/AddContract.vue"),
        meta: {
          title: "添加合同",
          isRoot:false,
        },
      }
    ]
  },
  
  //预览合同
  {
    path: '/contract/preview',
    name: "PreviewContract",
    component: () => import("@/views/contract/PreviewContract.vue"),
    meta: {
      title: "预览合同",
      isRoot:false,
    },
  },

  {
    path: "/customer",
    name: "Customer",
    component: () => import("@/layout/Index.vue"),
    redirect:"/customer/list",
    meta: {
      title:"客户管理"
    },
    children: [
      {
        path: "list",
        name: "CustomerList",
        component: () => import("@/views/customer/List.vue"),
        meta: {
          title: "客户列表",
          isRoot:true,
        },
      },
      {
        path: "search",
        name: "CustomerSearch",
        component: () => import("@/views/customer/Search.vue"),
        meta: {
          title: "客户搜索",
          isRoot:true,
        },
      },
    ]
  },
];


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});


router.beforeEach((to, from, next) => {
  
  const user: boolean = localStorage.getItem('userData') ? true : false
  // 本地无用户数据且不是跳转登录页则重定向至登录
  if (!user && to.path !== '/login') {
    next("/login")
    return
  }

  store.commit('breadCrumb/setBreadCrumb', to)

  next()

})

export default router;
